﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class DonViBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<DONVI> LayTatCa()
        {
            return DB.DONVIs.ToList();
        }

        public void Them(DONVI a)
        {
            DB.DONVIs.InsertOnSubmit(a);
            DB.SubmitChanges();
        }

        public void Xoa(int madv)
        {
            DONVI dv = DB.DONVIs.SingleOrDefault(a => a.MaDonVi == madv);
            DB.DONVIs.DeleteOnSubmit(dv);
            DB.SubmitChanges();
        }

        public void Sua(DONVI n)
        {
            DONVI o = DB.DONVIs.SingleOrDefault(a => a.MaDonVi == n.MaDonVi);
            o.TenDonVi = n.TenDonVi;
            o.MoTa = n.MoTa;
            DB.SubmitChanges();
        }

        public DONVI LayDonVi(int madv)
        {
            DONVI dv = DB.DONVIs.SingleOrDefault(a => a.MaDonVi == madv);
            return dv;
        }

        public string KiemTraTonTai(int _madv, string _tendv)
        {
            if (DB.DONVIs.Any(a => a.MaDonVi == _madv))
            {
                return "Mã Đơn Vị";
            }
            else if (DB.DONVIs.Any(b => b.TenDonVi == _tendv))
            {
                return "Tên Đơn Vị";
            }
            else
                return null;
        }
    }
}
